<?php
include_once("public.php");
/**
 * 输入提示信息
 *
 * @param unknown_type $no
 * @param unknown_type $msg
 */
function msg($no,$msg,$picid = 0){
	$json = new Services_JSON();
	$msg = array("no"=>$no,"msg"=>$msg,"picid"=>$picid);
	echo $json->encode($msg);
	die();
}
function update_pic($ID,$picid,$src,$idx){
	if($ID < 1) return;
	if($picid == 0){
		$sql = "insert into case_img(pid,img,idx) values('{$ID}','{$src}','{$idx}')";
		$GLOBALS['db']->query($sql);
		return $GLOBALS['db']->insert_id();
	}
	else{
		$sql = "update car_img set idx='{$idx}'";
		if($src!=''){
			$sql = "{$sql},img='{$src}' ";
		}
		$sql = "{$sql} where pid='{$ID}' and id='{$picid}'";
		$GLOBALS['db']->query($sql);
	}
}

set_time_limit(0);
//图片表单的名称
$filename = POSTS('filename');
//权重
$idx = POSTD('idx');
//产品编号
$ID = POSTD('ID');
//产品说明
$picid = POSTD('picid');
$cmd = POSTS('cmd');
if($cmd == 'delete'){
	$src = POSTS('src');
	if(substr($src,0,1) == "/")
		@unlink(DOCUMENTROOT.$src);
	else 
		@unlink(DOCUMENTROOT."/".$src);
	$db->query("delete from car_img where pid='{$ID}' and id='{$picid}'");
	
}
else{
	//判断是否上传图片
	if($_FILES["{$filename}"]["tmp_name"])
	{
		$tp = array("jpg","gif","png","jpeg","JPG","GIF","PNG","JPEG","bmp"); 
		$pathinfo = pathinfo($_FILES["{$filename}"]['name']);
		if(!isset($pathinfo['extension']) || !in_array($pathinfo['extension'],$tp)) { 
			msg(2,"只能上传的文件格式为jpg,gif,png！");
		}
		if($_FILES["{$filename}"]['size']>8000000){
			msg(3,"图片大小不能超过8M！");
		}
		$filepath = "/upload/case/{$ID}_" .date("YmdHis").".".$pathinfo['extension'];
		if(!@move_uploaded_file($_FILES["{$filename}"]["tmp_name"],DOCUMENTROOT . $filepath)){
			msg(4,"上传图片失败，稍后再试！");
		}
		$picid = update_pic($ID,$picid,$filepath,$idx);
		msg(0,$filepath,$picid);
	}elseif($picid > 0){
		update_pic($ID,$picid,'',$idx);
		msg(0,"保存成功");
	}
	else{
		msg(1,"请选择要上传的文件");
	}
}
?>